﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace HYElectricSetting
{
	public class CableDatabase
	{
		static CableDatabase()
		{
			CableDatabase.thisDb = new CableDatabase();
		}

		public static CableDatabase ThisDb
		{
			get
			{
				return CableDatabase.thisDb;
			}
		}

		private CableDatabase()
		{
			string dbFile = Path.Combine(Product.DataLocation, "ElectricalData", "CableManage.db");
			this.dbAssist = new SQLiteDatabaseAssist(dbFile);
		}

		public List<CableData> GetCableDatas()
		{
			List<CableData> list = new List<CableData>();
			List<CableData> result;
			try
			{
				string sqlStr = "select * from Cable";
				DataTable dataTable = this.dbAssist.ExecuteQuery(sqlStr);
				bool flag = dataTable == null;
				if (flag)
				{
					result = list;
				}
				else
				{
					foreach (object obj in dataTable.Rows)
					{
						DataRow dataRow = (DataRow)obj;
						list.Add(new CableData
						{
							CableName = dataRow["CableName"].ToString(),
							CableModel = dataRow["CableModel"].ToString(),
							CableSpec = dataRow["CableSpec"].ToString(),
							SystemType = dataRow["SystemType"].ToString(),
							OutDiameter = dataRow["OutDiameter"].ToString()
						});
					}
					result = list;
				}
			}
			catch (Exception)
			{
				MessageBox.Show("读取电缆数据失败，请检查数据源。", "盈建科提示");
				result = new List<CableData>();
			}
			return result;
		}

		public void SaveCableDatas(List<CableData> dataList)
		{
			bool flag = dataList == null || dataList.Count <= 0;
			if (!flag)
			{
				try
				{
					this.dbAssist.BeginTransaction();
					string text = "delete from Cable";
					this.dbAssist.ExecuteCommandEx(text);
					foreach (CableData cableData in dataList)
					{
						bool flag2 = string.IsNullOrWhiteSpace(cableData.CableModel) || string.IsNullOrWhiteSpace(cableData.CableSpec);
						if (!flag2)
						{
							text = string.Format("insert into Cable ({0},{1},{2},{3},{4}) ", new object[]
							{
								"CableName",
								"CableModel",
								"CableSpec",
								"SystemType",
								"OutDiameter"
							});
							text += string.Format("values ('{0}','{1}','{2}','{3}','{4}')", new object[]
							{
								cableData.CableName,
								cableData.CableModel,
								cableData.CableSpec,
								cableData.SystemType,
								cableData.OutDiameter
							});
							this.dbAssist.ExecuteCommandEx(text);
						}
					}
					this.dbAssist.CommitTransaction();
				}
				catch (Exception)
				{
					MessageBox.Show("保存电缆数据失败，请检查数据源。", "盈建科提示");
					this.dbAssist.RollbackTransaction();
				}
			}
		}

		public bool ContainCable(string cableModel, string cableSpec)
		{
			bool flag = string.IsNullOrEmpty(cableModel) || string.IsNullOrEmpty(cableSpec);
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				try
				{
					string sqlStr = string.Format("select * from Cable where {0}='{1}' and {2}='{3}'", new object[]
					{
						"CableModel",
						cableModel,
						"CableSpec",
						cableSpec
					});
					DataTable dataTable = this.dbAssist.ExecuteQuery(sqlStr);
					bool flag2 = dataTable == null;
					if (flag2)
					{
						result = false;
					}
					else
					{
						result = (dataTable.Rows.Count > 0);
					}
				}
				catch (Exception)
				{
					result = false;
				}
			}
			return result;
		}

		public double GetOutDiameter(string cableModel, string cableSpec)
		{
			double num = 0.0;
			bool flag = string.IsNullOrEmpty(cableModel) || string.IsNullOrEmpty(cableSpec);
			double result;
			if (flag)
			{
				result = num;
			}
			else
			{
				try
				{
					string sqlStr = string.Format("select * from Cable where {0}='{1}' and {2}='{3}'", new object[]
					{
						"CableModel",
						cableModel,
						"CableSpec",
						cableSpec
					});
					DataTable dataTable = this.dbAssist.ExecuteQuery(sqlStr);
					bool flag2 = dataTable == null || dataTable.Rows.Count <= 0;
					if (flag2)
					{
						result = num;
					}
					else
					{
						string text = dataTable.Rows[0]["OutDiameter"].ToString();
						bool flag3 = string.IsNullOrWhiteSpace(text);
						if (flag3)
						{
							result = num;
						}
						else
						{
							num = double.Parse(text.Trim());
							result = num;
						}
					}
				}
				catch (Exception)
				{
					result = num;
				}
			}
			return result;
		}

		private static CableDatabase thisDb = null;

		private SQLiteDatabaseAssist dbAssist = null;
	}
}
